import _ from 'lodash';
import { message, Select } from 'antd';
import { NAME_SPACE, SearchParams } from '../components/CarViolationManage/config'
import {  } from '../services/carViolationManage';

export default {
  namespace: NAME_SPACE,
  state:{
    loading: false,
    pageNo: 0,
    pageSize: 20,
    total: 0,
    searchParams: {...SearchParams},
    oldSearchParams: {...SearchParams},
    selectedRowKeys: [],
    // 详情可见
    modelVisable: true,
    details: {},
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({pathname, query}) => {
        if(pathname == '/carViolationManage'){
          dispatch({
            type:'init',
            payload:{}
          });
        }
      });
    },
  },
  effects: {
    *init({payload},{select, put, call}){
      // yield put({
      //   type: 'getList',
      //   payload: {type: 'search'}
      // })
    },
    *getList({payload},{select, put, call}){
      let {type: searchType, delNum} = payload;
      let {searchParams, oldSearchParams, total, pageSize, pageNo} = yield select(state => state[NAME_SPACE]);
      let newPageNo = 0, newPageSize = 20;
      switch (searchType) {
        case "search":
            oldSearchParams = Object.assign({}, searchParams);
            break;
        case "page":
            newPageNo = pageNo;
            newPageSize = pageSize;
        break;
            case "del":
            if (total===1 || (total-(pageNo)*pageSize - delNum)>0) {
                newPageNo = pageNo;
            }else{
                newPageNo = pageNo-1;
            }
            break;
        default :
            break;
      }
    },
  },
  reducers:{
    updateState(state, { payload }) {
      return { ...state, ...payload };
    },
    updateSubState(state, { payload }) {
        let { data, sub } = payload;
        return {
            ...state,
            [sub]: {
                ...state[sub],
                ...data,
            }
        }
    },
  },
}